package com.lei.zjs.web.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.jca.work.WorkManagerTaskExecutor;
import org.springframework.stereotype.Controller;

import com.lei.zjs.service.ebi.WorkordermanageService;
import com.lei.zjs.utils.BOSContext;
import com.lei.zjs.vo.Workordermanage;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 任务流程管理Action
 * 
 * @author Spinacia
 * 
 */
@Controller
@Scope("prototype")
public class TaskAction extends ActionSupport {

	// 注入流程引擎对象
	@Resource
	private ProcessEngine processEngine;
	@Resource
	private WorkordermanageService workordermanageService;
	
	//查询组任务
	public String findGroupTask(){
		TaskQuery taskQuery = processEngine.getTaskService().createTaskQuery();
		taskQuery.taskCandidateUser(BOSContext.getLoginUser().getId());
		List<Task> list = taskQuery.list();
		ActionContext.getContext().getValueStack().set("list", list);
		return "list";
	}
	
	private String taskId;
	
	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}
	
	public String getTaskId() {
		return taskId;
	}

	//加载任务变量数据
	public String showData() throws IOException{
		Map<String, Object> variables = processEngine.getTaskService().getVariables(taskId);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().print(variables);
		return NONE;
	}
	
	//拾取为个人任务
	public String takeTask(){
		processEngine.getTaskService().claim(taskId, BOSContext.getLoginUser().getId());
		return "toList";
	}
	
	//查看个人任务
	public String findPersonalTask(){
		TaskQuery taskQuery = processEngine.getTaskService().createTaskQuery();
		taskQuery.taskAssignee(BOSContext.getLoginUser().getId());
		List<Task> list = taskQuery.list();
		ActionContext.getContext().getValueStack().set("list", list);
		return "personList";
	}
	
	private String check;
	
	public void setCheck(String check) {
		this.check = check;
	}

	//办理工作单审核任务
	public String checkWorkOrderManage(){
		Map<String, Object> variables = processEngine.getTaskService().getVariables(taskId);
		Workordermanage workordermanage = (Workordermanage) variables.get("业务数据");
		if (check == null) {
			//没有审核
			ActionContext.getContext().getValueStack().set("map", variables);
			return "check";
		} else {
			workordermanage.setManagerCheck("1");
			workordermanageService.update(workordermanage);
			variables.put("业务数据", workordermanage);
			variables.put("check", check);
			processEngine.getTaskService().complete(taskId, variables);
			//有审核
			if ("0".equals(check)) {
				//审核不通过
				workordermanage.setStart(0);
				workordermanageService.update(workordermanage);
			}
		}
		return "toPersonList";
	}
	
	//办理其他任务
	public String outStore(){
		processEngine.getTaskService().complete(taskId);
		return "toPersonList";
	}
	
	public String transferGoods(){
		processEngine.getTaskService().complete(taskId);
		return "toPersonList";
	}
	
	public String receive(){
		processEngine.getTaskService().complete(taskId);
		return "toPersonList";
	}
}
